<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\circle;

use app\api\model\dao\Circle;
use app\api\model\dao\CircleCategory;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class AddCircleCate extends PageBase
{

    private $objCircleCate;

    public function __construct()
    {
        parent::__construct();

        $this->objCircleCate = new CircleCategory();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'name' => 'require',
            'rank' => 'require|number',
        ];
        $msg = [
            'rank.require' => '请填写排名',
            'name.require' => '圈子名称必须填写',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
//        $ret = $this->checkToken($param);
//        if (false === $ret) {
//            throw new Exception('check token fail...', Error::ERR_SYS);
//        }

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_CUSTOM, $ret);
        }

        $input = [
            'name' => $param['name'],
            'rank' => $param['rank'],
            'creator' => 70,
            'create_time' => time(),
        ];

        $ret = $this->objCircleCate->addCircleCategory($input);
        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();
    }
}
